package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.UUID;

import com.ruoyi.project.system.domain.MovieShowRelation;
import com.ruoyi.project.system.service.IMovieShowRelationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.Movie;
import com.ruoyi.project.system.service.IMovieService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 电影Controller
 * 
 * @author ruoyi
 * @date 2021-05-18
 */
@RestController
@RequestMapping("/system/movie")
public class MovieController extends BaseController
{
    @Autowired
    private IMovieService movieService;

    @Autowired
    private IMovieShowRelationService movieShowRelationService;

    /**
     * 查询电影列表
     */
    @PreAuthorize("@ss.hasPermi('system:movie:list')")
    @GetMapping("/list")
    public TableDataInfo list(Movie movie)
    {
        startPage();
        List<Movie> list = movieService.selectMovieList(movie);
        return getDataTable(list);
    }

    /**
     * 导出电影列表
     */
    @PreAuthorize("@ss.hasPermi('system:movie:export')")
    @Log(title = "电影", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Movie movie)
    {
        List<Movie> list = movieService.selectMovieList(movie);
        ExcelUtil<Movie> util = new ExcelUtil<Movie>(Movie.class);
        return util.exportExcel(list, "电影数据");
    }

    /**
     * 获取电影详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:movie:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(movieService.selectMovieById(id));
    }

    /**
     * 新增电影
     */
    @PreAuthorize("@ss.hasPermi('system:movie:add')")
    @Log(title = "电影", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Movie movie)
    {
        movie.setId(UUID.randomUUID().toString().replace("-", ""));
        int i = movieService.insertMovie(movie);
        MovieShowRelation movieShowRelation = new MovieShowRelation();
        movieShowRelation.setId(UUID.randomUUID().toString().replace("-", ""));
        movieShowRelation.setMovieId(movie.getId());
        movieShowRelation.setShowId(movie.getShowId());
        movieShowRelationService.insertMovieShowRelation(movieShowRelation);
        return toAjax(i);
    }

    /**
     * 修改电影
     */
    @PreAuthorize("@ss.hasPermi('system:movie:edit')")
    @Log(title = "电影", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Movie movie)
    {

        return toAjax(movieService.updateMovie(movie));
    }

    /**
     * 删除电影
     */
    @PreAuthorize("@ss.hasPermi('system:movie:remove')")
    @Log(title = "电影", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(movieService.deleteMovieByIds(ids));
    }
}
